package com.mengjiahui.auduoplayer.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mengjiahui.auduoplayer.R;
import com.mengjiahui.auduoplayer.adapter.MusicRankInfoAdapter;
import com.mengjiahui.auduoplayer.bean.Music;
import com.mengjiahui.auduoplayer.bean.MusicRank;
import com.mengjiahui.auduoplayer.service.NativeMusicService;
import com.mengjiahui.auduoplayer.service.OnlineMusicService;
import com.mengjiahui.auduoplayer.util.SpacesItemDecoration;

import java.io.Serializable;
import java.util.List;

/**
 * @author Meng
 * @data 2020/5/19
 */
public class MusicRankInfoActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Intent intent;
    private TextView musicRankInfoNameTextView;
    private TextView musicRankInfoUpdateTextView;
    private TextView musicRankInfoCommentTextView;
    private MusicRank musicRank;
    private List<Music> musicList;
    private RecyclerView recyclerView;
    public OnlineMusicService.MyBinder myBinder;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder=(OnlineMusicService.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_rank_info);
        init();
        assignment();
    }

    private void init() {
        toolbar=findViewById(R.id.toolbarMusicInfoHeader);
        musicRankInfoNameTextView=findViewById(R.id.textViewMusicRankInfoName);
        musicRankInfoUpdateTextView=findViewById(R.id.textViewMusicRankInfoUpdate);
        musicRankInfoCommentTextView=findViewById(R.id.textViewMusicRankInfoComment);
        recyclerView=findViewById(R.id.recyclerView2);
        intent=getIntent();
        musicRank=(MusicRank)intent.getSerializableExtra("musicRank");
        musicList=musicRank.getMusicList();
        toolbar.setTitle(musicRank.getRankName());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        MusicRankInfoAdapter musicRankInfoAdapter=new MusicRankInfoAdapter(musicList);
        recyclerView.setAdapter(musicRankInfoAdapter);
        recyclerView.addItemDecoration(new SpacesItemDecoration(8));
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(null);
        recyclerView.setLayoutManager(linearLayoutManager);
        musicRankInfoAdapter.setListener(music -> {
            Intent intent=new Intent(this, OnlineMusicService.class);
            intent.putExtra("music",(Serializable)musicList);
            intent.putExtra("CURRENT_POSITION",musicList.indexOf(music));
            startService(intent);
            bindService(intent,connection, Service.BIND_AUTO_CREATE);
        });
    }

    private void assignment() {
        musicRankInfoCommentTextView.setText(musicRank.getComment());
        musicRankInfoNameTextView.setText(musicRank.getRankName());
        musicRankInfoUpdateTextView.setText(musicRank.getUpdateDate());
    }
}
